﻿@using MvcApplication.Entity;
@model Post
@{
    ViewBag.Title = Model.ID == 0 ? "新建" : "编辑";
}
<style>
    .cover {
        max-width: 380px;
        max-height: 200px;
    }

    form.ke-upload-area.ke-form {
        width: 120px!important;
    }
</style>
@Scripts.Render("~/Content/kindeditor/kindeditor-all-min.js")
@Scripts.Render("~/Content/kindeditor/lang/zh_CN.js")
<form class="am-g" id="form">
    <input type="hidden" value="@Model.ArticleID" name="ArticleID" />
    <input type="hidden" value="@Model.ID" name="ID" />
    @{
        if (Model.ImageURLs == null)
        {
            Model.ImageURLs = string.Empty;
        }
        var imageUrls = Model.ImageURLs.Split(';');
        if (imageUrls.Length > 0)
        {
        <input type="hidden" id="imageURLs" value="@imageUrls[0]" name="ImageURLs" />
        }
        else
        { 
        <input type="hidden" id="imageURLs" value="" name="ImageURLs" />
        }
    }

    <div class="am-u-sm-8">
        <div class="column">
            <label>标题:</label>
            <input type="text" name="Title" value="@Model.Title" style="width:100%; padding:3px 5px;"/>
        </div>
        <div class="column">
            <label>内容:</label>
            <textarea id="detail" style="height:400px; width:100%;">@Html.Raw(Model.Detail)</textarea>
        </div>
    </div>
    <div class="am-u-sm-4">
        <label>@ViewBag.Title</label>
        <div style="height: 200px; border: 1px #eee dashed;" id="coverImageEl" class="column">
            @if (imageUrls.Length > 0)
            {
                <img class="cover" src="@imageUrls[0]" />       
            }
        </div>
        <input id="coverEl" value="选择封面" style="width: 200px;" />
        <div class="column" style="height: 175px; margin-top: 10px;">
            <div>
                <b>状态：</b>
                @if (Model.ID == 0)
                { 
                    <span>当前文章还没有发布到正式库</span>                    
                }
                else
                {
                    <span>已编辑,正式文章库ID：@Model.ID</span>
                }
            </div>
            <div class="col-2-1">
                <a href="/article/index/@Model.ArticleID" target="_blank">采集库文章预览</a>
            </div>
            <div class="col-2-1 last">
            </div>
        </div>
        <div>
            <a id="save" href="javascript:;" class="am-btn am-btn-success"><span>保存</span></a>
            <a id="saveThenBack" href="javascript:;" class="am-btn am-btn-success"><span>保存并返回</span></a>
            <a href="javascript:;" class="am-btn am-btn-secondary"><span>预览</span></a>
        </div>
    </div>
</form>

<script>
    KindEditor.ready(function (K) {
        window.editor = K.create('#detail', {
            width: '101%',
            height: '400px',
            uploadJson: '/image/index'
        });

        var uploadbutton = K.uploadbutton({
            button: K('#coverEl')[0],
            fieldName: 'imgFile',
            url: '/image/index',
            afterUpload: function (data) {
                if (data.error === 0) {
                    var url = K.formatUrl(data.url, 'absolute');
                    $('#coverImageEl').html('<img class="cover" src="{0}">'.replace('{0}', url));
                    $('#imageURLs').val(url);

                } else {
                    layer.msg('上传失败');
                }
            },
            afterError: function (str) {
                alert('自定义错误信息: ' + str);
            }
        });
        uploadbutton.fileBox.change(function (e) {
            uploadbutton.submit();
        });
    });
</script>
@Scripts.Render("~/Content/js/post/edit.js")
